import {isWeChat, storage} from '@/utils/tools'
import {OPEN_AUTH_URL} from '@/constants'
import Vue from 'vue'
import Router from 'vue-router'
const beforeEach = (to, from, next) => {



  const WX_UID = storage.get('WX_UID') || '';
  var ss = window.location;
  if(to.query.funCode =='yxc_pro'){

    next();

  }else if(!WX_UID && to.query.funCode !='yxc_pro'){
     if(/^(auth|TestCourseDetail|apply)$/.test(to.name)){
      // console.log(/^(Auth|invitation|apply)$/.test(to.name))
      next();
    }else{
      var xcode = getQueryString('code')
      if(!xcode){
        localStorage.setItem('llrouterUrl',ss);
      }
      // localStorage.setItem('furl',to.name);
      console.log('4'+'else')
      console.log('4'+getQueryString('code'))
      next({path:'/auth',query:{orgiPage:from.name}});

    }
  }
  next();

}


function getQueryString(name){
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  var q = window.location.pathname.substr(1).match(reg_rewrite);
  if(r != null){
    return unescape(r[2]);
  }else if(q != null){
    return unescape(q[2]);
  }else{
    return '';
  }
}



export default beforeEach
